package com.yanqu.road.dao.impl.activity.dafuweng;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionNews;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWUnionNewsDaoImpl extends TempDataObjectDao<DFWUnionNews> {

    public static String getDFWUnionNewsSql = "select * from t_c_activity_dfw_union_news where activityId=? and groupId=?";

    public Map<String, List<DFWUnionNews>> getDFWUnionNewsMap(int activityId, int groupId) {
        Map<String, List<DFWUnionNews>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDFWUnionNewsSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWUnionNews dFWUnionNews = parse(rs);
                if (!map.containsKey(dFWUnionNews.getUnionUid())) {
                    map.put(dFWUnionNews.getUnionUid(), new ArrayList<>());
                }
                map.get(dFWUnionNews.getUnionUid()).add(dFWUnionNews);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertDFWUnionNewsSql = "insert into t_c_activity_dfw_union_news(activityId,groupId,unionUid,bossId,createTime) values (?,?,?,?,?)";

    @Override
    protected boolean addData(DFWUnionNews dFWUnionNews) {
        dFWUnionNews.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionNews.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionNews.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dFWUnionNews.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUnionNews.getBossId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUnionNews.getCreateTime()));
        if (getDb().executeNoneQuery(insertDFWUnionNewsSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DFWUnionNews parseData(ResultSet rs) throws SQLException {
        DFWUnionNews dFWUnionNews = new DFWUnionNews();
        dFWUnionNews.setActivityId(rs.getInt("activityId"));
        dFWUnionNews.setGroupId(rs.getInt("groupId"));
        dFWUnionNews.setUnionUid(rs.getString("unionUid"));
        dFWUnionNews.setBossId(rs.getInt("bossId"));
        dFWUnionNews.setCreateTime(rs.getLong("createTime"));
        return dFWUnionNews;
    }

    @Override
    protected boolean updateData(DFWUnionNews data) {
        return false;
    }
}
